import java.sql.*;

// 只适用于单个数据表操作
public class sqlhelper {


    static Connection connection = null;
    static PreparedStatement preparedStatement = null;
    public static void register_drive(String key) throws ClassNotFoundException {
        Class.forName(key); // 反射注册驱动

    }

    public static void connect(String url, String user, String password) throws SQLException {
        connection = DriverManager.getConnection(url, user, password);
    }

    public static void close_commit() throws SQLException {
        connection.setAutoCommit(false); // 关闭自动提交事务
    }

    public static int delete(String sql) throws SQLException {
         preparedStatement = connection.prepareStatement(sql);
        return preparedStatement.executeUpdate();
    }

    public static Boolean insert(String sql) throws SQLException {
        preparedStatement = connection.prepareStatement(sql);
        return preparedStatement.execute();
    }

    public static ResultSet query(String sql) throws SQLException {
        preparedStatement = connection.prepareStatement(sql);
        return preparedStatement.executeQuery();
    }

    public static boolean update(String sql) throws SQLException {
        preparedStatement = connection.prepareStatement(sql);
        return preparedStatement.execute();
    }

    public static void commit() throws SQLException {
        connection.commit();
    }

    public static void close() throws SQLException {
        if (connection != null)
            connection.close();
        if (preparedStatement != null)
            preparedStatement.close();
    }
}
